package controllers

import (
	"ai/models"
	"ai/services"
	"net/http"

	"github.com/gin-gonic/gin"
)

type RiskController struct {
	riskService *services.RiskService
}

func NewRiskController() *RiskController {
	return &RiskController{
		riskService: services.NewRiskService(),
	}
}

func (c *RiskController) CreateRisk(ctx *gin.Context) {
	idCard := ctx.PostForm("idCard")
	name := ctx.PostForm("name")
	score := ctx.PostForm("score")

	risk := &models.Risk{
		IDCard: idCard,
		Name:   name,
		Score:  parseInt(score),
	}

	result, err := c.riskService.CreateOrGetRisk(risk)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": result,
	})
}
